<template>
  <el-menu
    :default-active="activeIndex"
    active-text-color="#ffd04b"
    :collapse="isCollapse"
    @select="handleSelect"
  >
    <li v-for="(item, index) in aSideData" :key="index">
      <el-submenu :index="index + ''" v-if="item.children !== undefined">
        <template slot="title" v-if="item.children !== undefined">
          <i :class="getClassName(item)"></i>
          <span>{{ item.title }}</span>
        </template>
        <el-menu-item-group>
          <template slot="title"></template>
          <li
            v-for="(itemChild, indexChild) in item.children"
            :key="indexChild"
          >
            <el-menu-item :index="index + '-' + indexChild"
              ><i :class="getClassName(itemChild)"></i>
              <span slot="title">{{ itemChild.title }}</span></el-menu-item
            >
          </li>
        </el-menu-item-group>
      </el-submenu>
      <el-menu-item :index="index + ''" v-else>
        <i :class="getClassName(item)"></i>
        <span slot="title">{{ item.title }}</span>
      </el-menu-item>
    </li>
  </el-menu>
</template>

<script lang="ts">
import { Vue, Component, Prop } from "vue-property-decorator";
import { ASideDataType } from "ASideTypes";
import { getRouteInfo } from "@/utils/index.ts";
import router from "@/router";
import { HeadModule } from "@/store/modules/HeadModule";
import { AsideModule } from "@/store/modules/AsideModule";

@Component({
  name: "Aside"
})
export default class Aside extends Vue {
  @Prop({ default: {} })
  private aSideData!: Array<ASideDataType>;
  isCollapse = false;

  get activeIndex() {
    return AsideModule.activeKey;
  }

  handleSelect(key: string, keyPath: Array<string>) {
    const info:
      | Pick<ASideDataType, "name" | "title">
      | false
      | undefined = getRouteInfo(keyPath);
    if (!info) return false;
    router.push({ name: info.name });
  }

  getClassName(data: ASideDataType) {
    return data.icon;
  }
}
</script>

<style>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
</style>
